/*
 * 作者：姚俊
 * 功能：对论著进行超级查询的action
 * 日期：2015-06-26
 */
package admin.action.superSearch;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import admin.action.fruits.Constant;
import admin.service.fruits.TreatiseService;

import teacher.bean.Treatise;
import tools.StringDate;

public class TreatiseSuperAction {
	
	private List<Treatise> treatises;
	private String date2;
	private Treatise treatise;
	private int direction;
	//当前页
	private int currentPage;
	//最后一页
	private int endPage;
	private int counts;
	private int flag;
	private int code;
	private String date;
	private String class_;
	
	


	public List<Treatise> getTreatises() {
		return treatises;
	}



	public void setTreatises(List<Treatise> treatises) {
		this.treatises = treatises;
	}



	public String getDate2() {
		return date2;
	}



	public void setDate2(String date2) {
		this.date2 = date2;
	}

	public Treatise getTreatise() {
		return treatise;
	}



	public void setTreatise(Treatise treatise) {
		this.treatise = treatise;
	}
	

	public int getDirection() {
		return direction;
	}



	public void setDirection(int direction) {
		this.direction = direction;
	}
	
	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getEndPage() {
		return endPage;
	}

	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}

	public int getCounts() {
		return counts;
	}

	public void setCounts(int counts) {
		this.counts = counts;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	public int getCode() {
		return code;
	}



	public void setCode(int code) {
		this.code = code;
	}

	public String getDate() {
		return date;
	}



	public void setDate(String date) {
		this.date = date;
	}



	public String getClass_() {
		return class_;
	}



	public void setClass_(String class_) {
		this.class_ = class_;
	}



	public String execute()
	{
		this.setFlag(4);
	List<String> orders=new ArrayList<String>();
		
		if(this.getTreatise()!=null)
		{
			if(this.getTreatise().getName()==null||this.getTreatise().getName().equals(""))
				this.getTreatise().setName(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getTreatise().setName(new String(this.getTreatise().getName().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getTreatise().getAuthor()==null||this.getTreatise().getAuthor().equals(""))
				this.getTreatise().setAuthor(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getTreatise().setAuthor(new String(this.getTreatise().getAuthor().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
	
			if(this.getTreatise().getBookNum()==null||this.getTreatise().getBookNum().equals(""))
				this.getTreatise().setBookNum(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getTreatise().setBookNum(new String(this.getTreatise().getBookNum().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getTreatise().getPress()==null||this.getTreatise().getPress().equals(""))
				this.getTreatise().setPress(null);
			else
			{
				if(this.getCode()!=1)
				{
					try {
						this.getTreatise().setPress(new String(this.getTreatise().getPress().getBytes("iso-8859-1"),"utf-8"));
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(this.getTreatise().getDate()!=null)
				this.getTreatise().setDate(
						StringDate.StringToDate(
								StringDate.DateToString(
										this.getTreatise().getDate())));
		}
		/**
		 * author:laohe;
		 */
		{
			if(this.getClass_()!=null&&!(this.getClass_().equals("")))
			{
				orders.add(this.getClass_());
			}
			if(this.getDate()!=null&&!(this.getDate().equals("")))
			{
				orders.add(this.getDate());
			}
		}
		//System.out.println("order尺寸："+orders.size());
		
		//当前页为零的时候，把当前页变成1
		if(this.getCurrentPage()==0)
			this.setCurrentPage(1);
		
		int add=0;
		//最后一页
		if(new TreatiseService().getTreatiseCountByDirection(
				this.getTreatise(), this.getDate2(),this.getDirection(),Constant.state)%Constant.pageSize!=0)
		{
			add=1;
		}

		this.setEndPage(
				new TreatiseService().getTreatiseCountByDirection(
						this.getTreatise(), this.getDate2(),this.getDirection(),Constant.state)/Constant.pageSize+add);
		this.setCounts(new TreatiseService().getTreatiseCountByDirection(
				this.getTreatise(), this.getDate2(), this.getDirection(),Constant.state));

		this.setTreatises(
		new TreatiseService().getSegregateTreatiseByDirection(
				this.getTreatise(), this.getDate2(),this.getDirection(),(this.getCurrentPage()-1)*Constant.pageSize+1,Constant.pageSize,Constant.state,orders));
		return "succ";
	}

}
